{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 保留字（关键字）\n",
    "使用Python标准库提供的keyword模块输出当前版本的所有关键字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']\n"
     ]
    }
   ],
   "source": [
    "import keyword\n",
    "print(keyword.kwlist)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 缩进\n",
    "Python使用缩进来表示代码块。缩进的空白数量是可变的，但是所有代码块语句必须包含相同的缩进空白数量，且同一个代码块使用相同的缩进。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个代码块\n",
      "第一个代码块\n"
     ]
    }
   ],
   "source": [
    "if True:\n",
    "    print(\"第一个代码块\")  \n",
    "    print(\"第一个代码块\")  \n",
    "else:\n",
    "    print(\"第二个代码块\")  \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 多行语句\n",
    "如果某个语句很长，则可以使用反斜杠”\\\\”来将一句语句分为多行显示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ab\n"
     ]
    }
   ],
   "source": [
    "s1,s2 = \"a\",\"b\"\n",
    "sum = s1 + \\\n",
    "\t  s2\n",
    "print(sum)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 多行语句\n",
    "Python中的注释分为单行注释和多行注释。单行注释以“#”开头，多行注释则使用“'''”或者“\"\"\"”环绕。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello World!\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\\n这是多行注释\\n这是多行注释\\n'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 这是单行注释，下面一行可以在终端输出\"Hello World!\"。\n",
    "print(\"Hello World!\")\n",
    "'''\n",
    "这是多行注释\n",
    "这是多行注释\n",
    "'''"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据类型\n",
    "Python中有五个标准数据类型，Numbers（数字）、String（字符串）、List（列表）、Tuple（元组）、Dictionary（字典）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数字\n",
    "Python支持四种不同的数字类型，分别是int（有符号整型）、bool（布尔型）、 float（浮点型）、complex（复数）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "整数: 42, 类型: <class 'int'>\n",
      "浮点数: 3.14159, 类型: <class 'float'>\n",
      "复数: (3+4j), 类型: <class 'complex'>\n",
      "复数的实部: 3.0, 虚部: 4.0\n",
      "布尔值True: True, 类型: <class 'bool'>\n",
      "布尔值False: False, 类型: <class 'bool'>\n",
      "布尔值作为整数: True = 1, False = 0\n"
     ]
    }
   ],
   "source": [
    "# 1. 整数类型(int)\n",
    "int_num = 42\n",
    "print(f\"整数: {int_num}, 类型: {type(int_num)}\")\n",
    "\n",
    "# 2. 浮点数类型(float)\n",
    "float_num = 3.14159\n",
    "print(f\"浮点数: {float_num}, 类型: {type(float_num)}\")\n",
    "\n",
    "# 3. 复数类型(complex)\n",
    "complex_num = 3 + 4j\n",
    "print(f\"复数: {complex_num}, 类型: {type(complex_num)}\")\n",
    "print(f\"复数的实部: {complex_num.real}, 虚部: {complex_num.imag}\")\n",
    "\n",
    "# 4. 布尔类型(bool) - 本质上是特殊的整数类型\n",
    "bool_true = True\n",
    "bool_false = False\n",
    "print(f\"布尔值True: {bool_true}, 类型: {type(bool_true)}\")\n",
    "print(f\"布尔值False: {bool_false}, 类型: {type(bool_false)}\")\n",
    "print(f\"布尔值作为整数: True = {int(bool_true)}, False = {int(bool_false)}\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串\n",
    "字符串是由字符组成的，Python中的字符串可以用单引号、双引号或三引号括起来。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这是一个字符串。\n",
      "这也一个字符串。\n",
      "这还是一个字符串。\n",
      "并且是多行字符串。\n"
     ]
    }
   ],
   "source": [
    "s1 = '这是一个字符串。'\n",
    "s2 = \"这也一个字符串。\"\n",
    "s3 = '''这还是一个字符串。\n",
    "并且是多行字符串。'''\n",
    "print(s1)\n",
    "print(s2)\n",
    "print(s3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以使用“+”拼接字符串。例如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这是一个字符串。这也一个字符串。这还是一个字符串。\n",
      "并且是多行字符串。\n"
     ]
    }
   ],
   "source": [
    "s = s1 + s2 + s3   #s的值为'这是一个字符串。这也是一个字符串。'\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python中字符串列表有2种取值顺序：从左到右索引默认0开始，最大范围是字符串长度少1；从右到左索引默认-1开始，最大范围是字符串开头。\n",
    "因此要实现从字符串中某一位置的字符，或者某一段子字符串，可以使用如下方式："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "bcde\n"
     ]
    }
   ],
   "source": [
    "a = 'abcdef'\n",
    "s0 = a[0] #s0的值是'a'\n",
    "s1 = a[1:5] #s1的值是'bcde'\n",
    "print(s0)\n",
    "print(s1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 列表\n",
    "列表是Python中最常用的数据类型之一。列表可以完成大多数集合类的数据结构实现。并且列表中可以存在不同的数据类型。列表用[]标识，用逗号分隔不同的数据项。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['student', 21, 'bill', 2023]\n",
      "student\n",
      "[21, 'bill']\n",
      "['bill', 2023]\n",
      "[123, 'bill', 123, 'bill']\n",
      "['student', 21, 'bill', 2023, 123, 'bill']\n"
     ]
    }
   ],
   "source": [
    "list = [ 'student', 21, 'bill', 2023 ] #list是一个列表\n",
    "tinylist = [123, 'bill'] \n",
    "print(list)             # 输出完整列表 \n",
    "print(list[0])          # 输出列表的第一个元素 \n",
    "print(list[1:3])        # 输出第二个至第三个元素 \n",
    "print(list[2:])         # 输出从第三个开始至列表末尾的所有元素 \n",
    "print(tinylist * 2)     # 输出列表两次 \n",
    "print(list + tinylist)  # 打印组合的列表\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 元组\n",
    "元组类似于列表，用()标识。内部元素用逗号隔开。元组不能二次赋值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tuple = ('student', 21, 'bill', 2023)\n",
    "tinytuple = (123, 'john')\n",
    " \n",
    "print(tuple)               # 输出完整元组\n",
    "print(tuple[0])            # 输出元组的第一个元素\n",
    "print(tuple[1:3])          # 输出第二个至第四个（不包含）的元素 \n",
    "print(tuple[2:])           # 输出从第三个开始至列表末尾的所有元素\n",
    "print(tinytuple * 2)       # 输出元组两次\n",
    "print(tuple + tinytuple)   # 打印组合的元组\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字典\n",
    "字典是无序的对象集合。字典当中的元素是通过键来存取的，而不是通过偏移存取。字典用{}标识，由索引和相应的值组成。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This is one\n",
      "This is two\n",
      "{'name': 'bill', 'code': 2023}\n",
      "dict_keys(['name', 'code'])\n",
      "dict_values(['bill', 2023])\n"
     ]
    }
   ],
   "source": [
    "dict = {}\n",
    "dict['one'] = \"This is one\"\n",
    "dict[2] = \"This is two\"\n",
    " \n",
    "tinydict = {'name': 'bill','code':2023}\n",
    "\n",
    "print(dict['one'])          # 输出键为'one' 的值\n",
    "print(dict[2])              # 输出键为 2 的值\n",
    "print(tinydict)             # 输出完整的字典\n",
    "print(tinydict.keys())      # 输出所有键\n",
    "print(tinydict.values())    # 输出所有值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 循环\n",
    "Python提供了for循环以及while循环\n",
    "## for循环\n",
    "for循环可以遍历列表、元组、字典、集合和字符串等数据类型。for循环的语法如下：\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前字母: F\n",
      "当前字母: r\n",
      "当前字母: u\n",
      "当前字母: i\n",
      "当前字母: t\n",
      "当前字母: s\n",
      "当前水果: apple\n",
      "当前水果: banana\n"
     ]
    }
   ],
   "source": [
    "for letter in 'Fruits':\n",
    "\tprint(\"当前字母: %s\" % letter)\n",
    "fruits = ['apple', 'banana']\n",
    "for fruit in fruits:\n",
    "   print ('当前水果: %s'% fruit)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## while循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "i = 1\n",
    "while i<10:\n",
    "\ti += 1\n",
    "\tif i%3 >0:\n",
    "\t\tcontinue\n",
    "\tif i > 7:\n",
    "\t\tbreak\n",
    "\tprint(i)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 函数\n",
    "函数是组织好的，可重复使用的，用来实现单一，或相关联功能的代码段。它能提高程序的模块性和代码的重复利用率。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Bill\n",
      "Bob\n"
     ]
    }
   ],
   "source": [
    "# 定义函数\n",
    "def printname(name):\n",
    "    '''打印传入的名字到标准显示设备上'''\n",
    "    print(name)\n",
    "    return\n",
    "\n",
    "# 调用函数\n",
    "printname(\"Bill\")\n",
    "printname(\"Bob\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
